package com.tang;

import lombok.extern.slf4j.Slf4j;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 具体的装饰实现类
 * 继承了抽象装饰类SsoDecorator,扩展方法preHandle
 * 在preHandle中，只关心扩展部分的功能，同时不会影响原有类的核心功能，也不会因为继承方式而导致的多余子类，增加了整体的灵活性。
 */
@Slf4j
public class LoginSsoDecorator extends SsoDecorator {

    private static final Map<String, String> authMap = new ConcurrentHashMap<>();

    static {
        authMap.put("huahua", "queryUserInfo");
        authMap.put("doudou", "queryUserInfo");
    }

    public LoginSsoDecorator(HandlerInterceptor handlerInterceptor) {
        super(handlerInterceptor);
    }

    @Override
    public boolean preHandle(String request, String response, Object handler) {
        boolean success = super.preHandle(request, response, handler);
        if (!success) return false;
        String userId = request.substring(8);
        String method = authMap.get(userId);
        return "queryUserInfo".equals(method);
    }

}
